#ifndef AVALON_TASKSIGNALHANDLER_HPP
#define AVALON_TASKSIGNALHANDLER_HPP
#include "Avalon/SignalHandling/QueuedSignalHandler.hpp"
#include "Avalon/Threading/Task.hpp"

namespace Avalon {
namespace SignalHandling {

  /*! \class TaskSignalHandler
   *  \brief Allows for the handling of signals in a concurrent Task.
   *  \author Kamal Mansouri <kranar@hotmail.com>
   */
  class TaskSignalHandler {
    public:

      //! Constructs a TaskSignalHandler.
      TaskSignalHandler();

      ~TaskSignalHandler();

      //! Starts handing incoming signals.
      void Start();

      //! Stops handling signals.
      void Stop();

      //! Returns a slot compatible with this signal handler.
      /*!
        \param slot The slot to make compatible with this signal handler.
        \return A slot compatible with this signal handler.
      */
      template<typename Signal>
      typename Signal::slot_type GetSlot(
          const typename Signal::slot_type& slot) {
        return m_queuedSignalHandler->GetSlot<Signal>(slot);
      }

    private:
      class SignalHandlerTask : public Threading::Task {
        public:
          SignalHandlerTask();

          virtual ~SignalHandlerTask();

          void SetSignalHandler(TaskSignalHandler* signalHandler);

        protected:
          virtual void ProcessTask();

        private:
          TaskSignalHandler* m_signalHandler;
      };
      boost::mutex m_mutex;
      QueuedSignalHandlerPtr m_queuedSignalHandler;
      boost::signals2::scoped_connection m_queuedConnection;
      SignalHandlerTask m_task;

      void OnSignalsQueued();
  };
}
}

#endif // AVALON_TASKSIGNALHANDLER_HPP
